//
//  LoginMainView.m
//  AppleMarket
//
//  Created by yangy on 22/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "LoginMainView.h"

// view
#import "BH_UnderLineTextField.h"
// videModel
#import "LoginViewModel.h"
#import "LoginStrategyModel.h"


@interface LoginMainView()
@property (nonatomic, strong) UIImageView *logoImageView;   // logo图标
@property (nonatomic, strong) UILabel *logoLabel;
@property (nonatomic, strong) BH_UnderLineTextField *accountTF;     // 账号
@property (nonatomic, strong) BH_UnderLineTextField *passwordTF;    // 密码
@property (nonatomic, strong) BH_UnderLineTextField *codeTF;    // 密码
@property (nonatomic, strong) UIButton *loginButton;   // 登录
@property (nonatomic, strong) UIButton *forgetButton; // 忘记密码
@property (nonatomic, strong) UIButton *eyeButton;
@property (nonatomic, strong) LoginViewModel *viewModel;
@property (nonatomic, strong) UIButton *codeButton; // 图形验证码
@end

@implementation LoginMainView

- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel;
{
    self = [super initWithFrame:frame];
    if(self){
        // 开始结束事件
        _viewModel = viewModel;
        [self createUI];
        [self bindVM];
    }
    return self;
}

- (void) bindVM
{
    RAC(self.viewModel,accountStr_) = _accountTF.rac_textSignal;
    RAC(self.viewModel,passwordStr_) = _passwordTF.rac_textSignal;
    RAC(self.loginButton,enabled) = self.viewModel.loginButtonEnableSignal;
    RAC(self.loginButton,backgroundColor) = self.viewModel.loginButtonColorSignal;
}

- (void) createUI
{
    [self addSubview:self.logoImageView];
    [self addSubview:self.logoLabel];
    [self addSubview:self.accountTF];
    [self addSubview:self.passwordTF];
    [self addSubview:self.loginButton];
    [self addSubview:self.codeTF];
    [self addSubview:self.forgetButton];
    [self setViewsLayout];
}

- (void) setViewsLayout
{
    [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(31);
        make.top.mas_equalTo(self).offset(31);
    }];
    
    [_logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoImageView);
        make.top.mas_equalTo(self.logoImageView.mas_bottom).offset(7);
    }];
    
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoImageView);
        make.right.mas_equalTo(self).offset(-31);
        make.top.mas_equalTo(self.logoLabel.mas_bottom).offset(66);
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoImageView);
        make.right.mas_equalTo(self.accountTF);
        make.top.mas_equalTo(self.accountTF.mas_bottom).offset(18);
    }];
    
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passwordTF.mas_bottom).offset(51);
        make.left.mas_equalTo(self.passwordTF.mas_left);
        make.right.mas_equalTo(self.passwordTF.mas_right);
        make.height.mas_equalTo(40);
    }];
    
    [_forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.passwordTF.mas_left);
        make.top.mas_equalTo(self.loginButton.mas_bottom).offset(21);
    }];
}

- (UIImageView *)logoImageView
{
    if(!_logoImageView){
        _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    }
    return _logoImageView;
}

- (UILabel *)logoLabel
{
    if(!_logoLabel){
        _logoLabel = [UILabel cm_labelWithText:@"登录苹果数字资产交易平台" font:kFont(14) textColor:kColorWithHex(0xADB7C1)];
    }
    return _logoLabel;
}

- (BH_UnderLineTextField *)accountTF
{
    if(!_accountTF){
        _accountTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _accountTF.font = kFont(13);
        _accountTF.placeholder = @"手机号码/邮箱账号";
        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTF.autocapitalizationType = UITextAutocapitalizationTypeNone;        //首字母不大写
        [[_accountTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
            if(!kStringIsEmpty(self.accountTF.error)){
                self.accountTF.error = @"";
            }
            return value.length >= 0;
        }] subscribeNext:^(NSString * _Nullable x) {
            
        }];
    }
    return _accountTF;
}

- (BH_UnderLineTextField *)passwordTF
{
    if(!_passwordTF){
        _passwordTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _passwordTF.font = kFont(13);
        _passwordTF.placeholder = @"密码";
        _passwordTF.secureTextEntry = YES;
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.autocapitalizationType = UITextAutocapitalizationTypeNone;        //首字母不大写
        _passwordTF.rightView = self.eyeButton;
        _passwordTF.rightViewMode = UITextFieldViewModeWhileEditing;
        //_passwordTF.error = @"你好";
        [[_passwordTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
            if(kStringIsEmpty(self.passwordTF.error)){
                self.passwordTF.error = @"";
            }
            return value.length >= 0;
        }] subscribeNext:^(NSString * _Nullable x) {
            
        }];
    }
    return _passwordTF;
}

- (UIButton *)loginButton
{
    if(!_loginButton){
        _loginButton = [UIButton cm_ButtonWithTitle:@"登录" font:kFont(14) titleColor:[UIColor whiteColor]];
        _loginButton.backgroundColor = kColorWithHex(0xADB7C1);
        //_loginButton.enabled = NO;
        @weakify(self);
        [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self endEditing:YES];
            if(![self.accountTF.text bh_IsValidPhoneNumber] && ![self.accountTF.text bh_IsEmailAddress]){
                self.accountTF.error = @"输入的账号格式有误";
            }else if((self.passwordTF.text.length < 6 || self.passwordTF.text.length > 20)){
                self.passwordTF.error = @"密码为6~20位";
            }else{
                [[self.viewModel.checkCommond execute:nil] subscribeNext:^(id  _Nullable x) {
                    
                } error:^(NSError * _Nullable error) {
                    NSLog(@"网络错误");
                }];
            }
        }];
    }
    return _loginButton;
}

- (UIButton *)forgetButton
{
    if(!_forgetButton){
        _forgetButton = [UIButton cm_ButtonWithTitle:@"忘记密码?" font:kFont(12) titleColor:kColorWithHex(0x3291EB)];
        @weakify(self);
        [[_forgetButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.viewModel.forgetPwdClickSubject sendNext:nil];
        }];
    }
    return _forgetButton;
}

- (UIButton *)eyeButton
{
    if(!_eyeButton){
        _eyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_eyeButton setImage:[UIImage imageNamed:@"eye"] forState:UIControlStateNormal];
        _eyeButton.frame = CGRectMake(0, 0, 44, 44);
        @weakify(self);
        [[_eyeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            x.selected = !x.isSelected;
            self.passwordTF.secureTextEntry = !x.isSelected;
            self.passwordTF.text = [self.passwordTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        }];
    }
    return _eyeButton;
}

@end
